﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rename.UC
{
    public class UcComboBox: ComboBox
    {
        private Color _borderColor = Color.DarkGray;
        private int _borderWidth = 1;
        private const int WM_PAINT = 0xF;
        private bool _suppressSelectionEvent = false;

        public UcComboBox()
        {
            DropDownStyle = ComboBoxStyle.DropDownList;
            FlatStyle = FlatStyle.Popup;
            DoubleBuffered = true;
            Margin = new Padding(0, 0, 0, 0);
            if (SelectedIndex > 0)
            {
                SelectedIndex = 0;
            }
            MouseEnter += OnMouseEnter;
            MouseLeave += OnMouseLeave;
        }

        #region  自定义控件属性
        [Category("自定义属性")]
        [Description("获取或设置控件的颜色")]
        [DefaultValue(typeof(Color), "Red")]
        public Color BorderColor
        {
            get => _borderColor;
            set
            {
                _borderColor = value;
                base.Invalidate();
            }
        }
        [Category("自定义属性")]
        [Description("获取或设置控件的边框")]
        [DefaultValue(1)]
        public int BorderWidth
        {
            get => _borderWidth;
            set
            {
                _borderWidth = value;
                base.Invalidate();
            }
        }
        #endregion

        #region 重写虚函数
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_PAINT)
            {
                WmPaint(ref m);
            }
        }
        // 当控件大小改变时重绘
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Invalidate();
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
           // if (DesignMode) return;
            // 确保有项且当前没有选中项
            if (Items.Count > 0 && SelectedIndex == -1 && IsHandleCreated)
            {
                try
                {
                    _suppressSelectionEvent = true;
                    // 选择第一项
                    SelectedIndex = 0;
                }
                finally
                {
                    _suppressSelectionEvent = false;
                }
            }
        }

        #endregion
        private void WmPaint(ref Message m)
        {
            //Console.WriteLine(m.ToString());
            using (Graphics graphics = Graphics.FromHwnd(base.Handle))
            {
                // 绘制边框
                using (Pen pen = new Pen(_borderColor, _borderWidth))
                {
                    //Console.WriteLine(_borderColor.ToString());
                    // 计算边框矩形，考虑边框宽度
                    Rectangle borderRect = new Rectangle(_borderWidth / 2, _borderWidth / 2,ClientSize.Width - _borderWidth, ClientSize.Height - _borderWidth);
                    graphics.DrawRectangle(pen, borderRect);
                }
            }
        }

        private void OnMouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Default;
        }

        private void OnMouseEnter(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }
    }
}
